// 导入提示方法
import { message } from "ant-design-vue";

export const useUpload = (addModel, swipe_image_src) => {
  // 上传文件改变时的状态
  const handleChange = (info) => {
    // 如果addModel.swipe_image存储图片的长度不为0 则让它只存第一张
    if (addModel.swipe_image.length !== 0) {
      addModel.swipe_image = [addModel.swipe_image[0]];
    }

    // 上传完成
    if (info.file.status === "done") {
      if (info.file.response.code === 200) {
        addModel.swipe_image = info.fileList;
        swipe_image_src.value = info.file.response.data.filename;
        message.success("上传成功");
      }
    } else if (info.file.status === "error") {
      // 上传失败
      return message.error(`${info.file.name} file upload failed.`);
    }
  };

  // 上传文件之前的钩子
  const beforeUpload = () => {
    if (addModel.swipe_image.length !== 0) {
      return false;
    }
  };

  return {
    handleChange,
    beforeUpload,
  };
};
